<?php

namespace App\Exceptions;

use App\Enums\ResponseEnum;
use App\Traits\ResponseTrait;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Throwable;

class Handler extends ExceptionHandler
{
    use ResponseTrait;
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * @param $request
     * @param AuthenticationException $exception
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        return $this->error(ResponseEnum::CLIENT_HTTP_UNAUTHORIZED);
    }

    /**
     * @param $request
     * @param ValidationException $exception
     * @return \Illuminate\Http\JsonResponse
     */
    protected function invalidJson($request, ValidationException $exception): \Illuminate\Http\JsonResponse
    {
        return $this->error($exception->status, $exception->getMessage());
    }

    /**
     * @param $request
     * @param Throwable $e
     * @return \Illuminate\Http\JsonResponse
     */
    protected function prepareJsonResponse($request, Throwable $e): \Illuminate\Http\JsonResponse
    {
        return $this->error($this->isHttpException($e) ? $e->getCode() : ResponseEnum::SYSTEM_ERROR, $this->isHttpException($e) ? $e->getMessage() : 'Server Error');
    }
}
